package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.ExhibitionRegistrationBean;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.ui.view.CommonDialogUtils;
import com.dinadi.yidi.utils.FormatCurrentData;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.zxing.EncodingUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * author: JiaYu.He
 * Date: 2017/6/28  查看订单信息
 * Time: 10:59
 */
public class OrderInformationActivity extends BaseActivity {
    private View view;
    private TextView tvTitle, text_title_right;
    private TextView user_phone, username, title_competition, time_competition, place_competition, sign_price, order_number;
    private RelativeLayout exhibition_details;
    private ImageView qr_code, btn_operation, information_state;
    /*联系电话*/
    private CommonDialogUtils dialog;
    /*取消报名*/
    private CommonDialogUtils canelDialog;
    private String showUserLogId, showId, token;
    /*显示logo*/
    private boolean isShow = false;

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(OrderInformationActivity.this).inflate(R.layout.activity_order_information, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(getResources().getString(R.string.text_dingdan));
        text_title_right = (TextView) view.findViewById(R.id.text_title_right);
        text_title_right.setText(getResources().getString(R.string.text_kefu));
        user_phone = (TextView) view.findViewById(R.id.user_phone);
        username = (TextView) view.findViewById(R.id.username);
        btn_operation = (ImageView) view.findViewById(R.id.btn_operation);
        title_competition = (TextView) view.findViewById(R.id.title);
        time_competition = (TextView) view.findViewById(R.id.time_competition);
        place_competition = (TextView) view.findViewById(R.id.place_competition);
        sign_price = (TextView) view.findViewById(R.id.sign_price);
        qr_code = (ImageView) view.findViewById(R.id.qr_code);
        order_number = (TextView) view.findViewById(R.id.order_number);
        exhibition_details = (RelativeLayout) view.findViewById(R.id.exhibition_details);
        information_state = (ImageView) view.findViewById(R.id.information_state);
    }

    @Override
    protected void initListener() {
        registerClickListener(btn_operation, text_title_right, exhibition_details);
    }

    @Override
    protected void initData() {
        token = SharePreferencesHelper.getInstance(OrderInformationActivity.this).getString("token");
        Intent intent = getIntent();
        showUserLogId = intent.getStringExtra("showUserLogId");
        order_number.setText(showUserLogId);
                    /*二维码的颜色*/
        EncodingUtils.qrCode(0xff000000);
                 /*二维码里边的颜色*/
        EncodingUtils.inQRCode(0xffffffff);
        Bitmap qrCode = EncodingUtils.createQRCode(showUserLogId, 500, 500,
                isShow ? BitmapFactory.decodeResource(getResources(), R.drawable.app_icon) : null);
        qr_code.setImageBitmap(qrCode);
        ExhibitionRegistrationBean bean = new ExhibitionRegistrationBean();
        bean.setShowUserLogId(showUserLogId);
        bean.setToken(token);
        CommonInterface exhibitionRegistrationInterface = new CommonInterface(OrderInformationActivity.this, httpListener);
        exhibitionRegistrationInterface.getInterface("12008", JSON.toJSONString(bean));


    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.btn_operation:
                /*取消报名操作*/

                canelDialog = new CommonDialogUtils(OrderInformationActivity.this);
                canelDialog.setContent(R.string.text_canle);
                canelDialog.setDialogCallback(dialogcallback3);
                canelDialog.show();
                break;
            case R.id.exhibition_details:
                /*进入展赛详情*/
                Intent intent = new Intent(OrderInformationActivity.this, ExhibitionMatchActivity.class);
                intent.putExtra("showId", showId);
                startActivity(intent);
                break;
            case R.id.text_title_right:
                /*客服*/
                dialog = new CommonDialogUtils(OrderInformationActivity.this);
                dialog.setContent(R.string.telephone_numbers);
                dialog.setDialogCallback(dialogcallback2);
                dialog.show();
                dialog.determineText(R.string.tell);
                break;
            default:
                break;

        }
    }

    HttpListener httpListener = new HttpListener() {
        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                showId = JsonUtil.getFieldValue(response, "showId");
                String user = JsonUtil.getFieldValue(response, "signName");
                String phone = JsonUtil.getFieldValue(response, "signPhone");
                username.setText(user);
                user_phone.setText(phone);
                String title = JsonUtil.getFieldValue(response, "title");
                String signPrice = JsonUtil.getFieldValue(response, "signPrice");
                if (signPrice.equals("0")) {
                    sign_price.setText("免费");
                } else {
                    sign_price.setText(signPrice);
                }
                String address = JsonUtil.getFieldValue(response, "address");
                String startTime = JsonUtil.getFieldValue(response, "startTime");
                //   String startDate = DateFormat.getDateInstance(DateFormat.FULL).format(FormatCurrentData.strToDateLong(startTime));
                String endTime = JsonUtil.getFieldValue(response, "endTime");
                //  String endDate = DateFormat.getDateInstance(DateFormat.FULL).format(FormatCurrentData.strToDateLong(endTime));
//                time_competition.setText(startDate.substring(0, startDate.length() - 3) + "-" + endDate.substring(0, endDate.length() - 3));
                String startDate = FormatCurrentData.getDate(startTime);
                String endDate = FormatCurrentData.getDate(endTime);
                time_competition.setText(startDate + " 到 " + endDate);

                place_competition.setText(address);
                title_competition.setText(title);

                /*1-未核销 2-已核销 3-已取消"*/
                String verificationStatus = JsonUtil.getFieldValue(response, "verificationStatus");
                /*報名状态*/
                String signStatus = JsonUtil.getFieldValue(response, "signStatus");
                Date date = new Date();
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = format.format(date);
/*两个Date类型的变量可以通过compareTo方法来比较。此方法的描述是这样的：如果参数 Date 等于此 Date，则返回值 0；如果此 Date 在 Date 参数之前，则返回小于 0 的值；如果此 Date 在 Date 参数之后，则返回大于 0 的值。
实际上比较的是自1970 年 1 月 1 日 00:00:00 GMT以来的毫秒数。毫秒数越大的时间越大。*/
                String DateStr1 = time;
                String DateStr2 = startTime;
                String DateStr3 = endTime;
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date dateTime1 = null;
                try {
                    dateTime1 = dateFormat.parse(DateStr1);
                    Date dateTime2 = dateFormat.parse(DateStr2);
                    Date dateTime3 = dateFormat.parse(DateStr3);
                    int i = dateTime1.compareTo(dateTime2);
                    int j = dateTime1.compareTo(dateTime3);

                    if (j == 0) {
                        /*两个时间一样*/
                    } else if (j < 0) {
                        /*活动没有结束*/
                        if (i == 0) {
                        /*两个时间一样*/

                        } else if (i < 0) {

/*"报名状态[int]，2：已报名 3：已取消",*/
                            if(signStatus.equals("2")){
                                 /*活动还没有开始*/
                                if (verificationStatus.equals("1")) {
                    /*可以取消报名*/
                                    btn_operation.setImageResource(R.drawable.details_tab_cancel);
                                } else if (verificationStatus.equals("2")) {
                    /*已完成*/
                                    btn_operation.setVisibility(View.GONE);
                                    information_state.setVisibility(View.VISIBLE);
                                }
                            }else{
                                 /*已经取消了*/
                                btn_operation.setOnClickListener(null);
                                btn_operation.setImageResource(R.drawable.details_tab_complete_cancellation);

                            }
/*
                        *//*活动还没有开始*//*
                            if (verificationStatus.equals("1")) {
                    *//*可以取消报名*//*
                                btn_operation.setImageResource(R.drawable.details_tab_cancel);
                            } else if (verificationStatus.equals("2")) {
                    *//*已完成*//*
                                btn_operation.setVisibility(View.GONE);
                                information_state.setVisibility(View.VISIBLE);
                            } else {
                    *//*已经取消了*//*
                                btn_operation.setOnClickListener(null);
                                btn_operation.setImageResource(R.drawable.details_tab_complete_cancellation);

                            }*/
                        } else {
                            //*活动已开始*//
                            btn_operation.setImageResource(R.drawable.activity_tab_already_begun);
                            btn_operation.setOnClickListener(null);
                        }
                    } else {
                        //*活动已结束*//
                        btn_operation.setImageResource(R.drawable.activity_tab_is_over);
                        btn_operation.setOnClickListener(null);
                    }
                    System.out.println(i < 0);
                } catch (ParseException e) {
                    e.printStackTrace();
                }


            } else {
                toast(errMsg);
            }
        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
        }
    };

    HttpListener httpListener2 = new HttpListener() {
        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                toast("报名取消");
                btn_operation.setOnClickListener(null);
                btn_operation.setImageResource(R.drawable.details_tab_complete_cancellation);
            }
        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            toast(errorResponse);
        }
    };

    /**
     * 设置mydialog需要处理的事情
     */
    CommonDialogUtils.Dialogcallback dialogcallback2 = new CommonDialogUtils.Dialogcallback() {
        @Override
        public void dialogdo() {
            Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:15101570380"));// 跳转到拨号界面，同时传递电话号码
            startActivity(dialIntent);
            dialog.dismiss();
        }
    };
    /**
     * 设取消报名
     */
    CommonDialogUtils.Dialogcallback dialogcallback3 = new CommonDialogUtils.Dialogcallback() {
        @Override
        public void dialogdo() {
            ExhibitionRegistrationBean bean = new ExhibitionRegistrationBean();
            bean.setShowUserLogId(showUserLogId);
            bean.setToken(token);
            CommonInterface exhibitionRegistrationInterface = new CommonInterface(OrderInformationActivity.this, httpListener2);
            exhibitionRegistrationInterface.getInterface("12009", JSON.toJSONString(bean));
            canelDialog.dismiss();
        }
    };
}
